<?php

namespace App\Transformers\Order;

use App\Models\Order;
use App\Transformers\User\UserTransformer;
use League\Fractal\TransformerAbstract;

class OrderIndexTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['orderCartInfo','user'];

    public function transform(Order $data)
    {
        return [
            'id' => $data->id,
            'user_id' => $data->user_id ?? '',
            'promoter_id' => $data->promoter_id ?? '',
            'order_number' => $data->order_number ?? '',
            'type' => $data->type ?? '',
            'type_text' => $data->orderTypeEnum[$data->type] ?? '',
            'total_price' => $data->total_price ?? '',
            'pay_price' => $data->pay_price ?? '',
            'pay_time' => $data->pay_time ?? '',
            'pay_type' => $data->pay_type ?? '',
            'pay_type_text' => $data->orderPayType[$data->pay_type] ?? '',
            'status' => $data->status ?? '',
            'status_text' => $data->orderStatusTypeEnum[$data->status] ?? '',
            'fictitious_content' => $data->fictitious_content ?? '',
            'paid' => $data->paid ?? 0,
            'refund_status' => $data->refund_status ?? '',
            'refund_status_text' => $data->refundStatusTypeEnum[$data->refund_status] ?? '',
            'refund_reason_wap_img' => $data->refund_reason_wap_img ?? '',
            'refund_reason_wap_explain' => $data->refund_reason_wap_explain ?? '',
            'mark' => $data->mark ?? '',
            'total_postage' => $data->total_postage ?? '',
            'pay_postage' => $data->pay_postage ?? '',
            'total_num' => $data->total_num ?? '',
            'is_user_delete' => $data->is_user_delete ?? '',
            'refund_reason_time' => $data->refund_reason_time ?? '',
            'orderCartInfo' => $data->orderCartInfo ?? '',
            'user' => $data->user ?? '',
            'deleted_at' => $data->deleted_at ? $data->deleted_at->format("Y-m-d H:i:s") : '',
            'created_at' => $data->created_at ? $data->created_at->format("Y-m-d H:i:s") : '',
            'updated_at' => $data->updated_at ? $data->updated_at->format("Y-m-d H:i:s") : '',
        ];
    }

    /**
     * 订单用户信息
     */
    public function includeUser(Order $order)
    {
        return $this->item($order->user,new UserTransformer());
    }

    /**
     * 订单的购物车信息
     */
    public function includeOrderCartInfo(Order $order)
    {
        return $this->collection($order->orderCartInfo, new OrderCartInfoTransformer());
    }

}
